package ipcards;
import java.util.*;


public class CardSetControl extends ArrayList<CardSet> {

	private ArrayList<CardSet> groups = new ArrayList<CardSet>();
	private Map<Card,CardSet> cardgroup = new HashMap<Card,CardSet>();
	
	

	CardSet allcards;
	

	public CardSetControl(CardSet listOfAllCards) {
		allcards = listOfAllCards;
	}

		
	
	private void rebuildGroups() {
		groups = new ArrayList<CardSet>();
		cardgroup = new HashMap<Card,CardSet>();
		for (Card c: allcards) {
			ArrayList<CardSet> overlapgroups = new ArrayList<CardSet>();	// list of groups this card overlaps with
			for(Card other: cardgroup.keySet()) {
				if (c.overlaps(other)) {
					CardSet g = cardgroup.get(other);
					if(!overlapgroups.contains(g)) {
						overlapgroups.add(g);
					}
				}
			}
			CardSet mergegroup = new CardSet(c);	// new group, that has the new card in it
			for(CardSet g: overlapgroups) {
				mergegroup.addAll(g);
				groups.remove(g);
			}
			groups.add(mergegroup);
			for(Card cd: mergegroup) {
				cardgroup.put(cd,mergegroup);
			}
		}
	}
	
	
	public Collection<CardSet> getGroups() {
		return groups;
	}
	
	/*public void add(Card c) {
		
	}
	
	public void add(Group g) {
		
	}
	
	public Card remove(int cardID) {
		return null;
	}
	
	public Card[] remove(int[] cardIDs) {
		return null;
	}*/
	
	/*	

	private ArrayList<CardSet> setlist;
	
	public CardSetControl(Collection<CardSet> initial) {
		super();
		setlist = new ArrayList<CardSet>(initial);
*/	
	/*	public CardSetControl(Collection<Card> initial) {
		super(initial);
//>>>>>>> .r297
	}
/*	public CardSetControl(CardSet initial) {
		super();
		setlist = new ArrayList<CardSet>();
		setlist.add(initial);
	}
	public CardSetControl() {
		super();
		setlist = new ArrayList<CardSet>();
	}*/
	
/*<<<<<<< .mine
	public void rebuildgroups() {
//=======*/
}